package cn.com.rpc.invoke;

import cn.com.common.Invocation;
import cn.com.common.Result;
import cn.com.common.URI;
import cn.com.common.exception.RpcException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @param <T>
 * @author jiaming
 */
public abstract class AbstractInvoker<T> implements Invoker<T> {

    protected static final Logger log = LogManager.getLogger();

    private final URI uri;

    public AbstractInvoker(URI uri) {
        this.uri = uri;
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public Result invoke(Invocation invocation) {
        //获取参数
        Result result = null;
        try {
            result = doInvoke(invocation, uri);
        } catch (Exception e) {
            throw new RpcException(e.getMessage(), e);
        }
        return result;
    }

    @Override
    public URI getUri() {
        return uri;
    }

    @Override
    public void destory() {

    }

    public abstract Result doInvoke(Invocation invocation, URI uri);
}
